package algorithms.que14;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @功能  (lgN)^2-(1+2+3+.....+lgN)
 * @创建时间 2020/9/10
 */
public class E1d4d20 {
   AtomicInteger ww=new AtomicInteger(0);

    public void getadd(int[] arr,int a,int la,int fi){
        if(la==0&&fi==0){
            la=arr.length-1;
            fi=0;}
        while (fi<la){
            int mid =fi+(la-fi)/2;
            if(arr[mid]>=a&&arr[mid]>=arr[fi]&&arr[mid]<=arr[la]){
                la=mid-1;
                continue;
            }else if(arr[mid]<=a&&arr[mid]>=arr[fi]&&arr[mid]<=arr[la]){
                fi=mid+1;
                continue;
            }else if(arr[mid]==a){
                System.out.println(mid);
                break;
            }
            else if(arr[mid]<=arr[fi]&&arr[mid]>=arr[la]){
                getdec( arr, a, la, fi);
                break;
            }else if(arr[mid]>=arr[fi]&&arr[mid]>=arr[la]){
                getdec( arr, a, la, mid);
                getadd( arr, a, mid, fi);
                break;
            }else if(arr[mid]<=arr[fi]&&arr[mid]<=arr[la]){
                getdec( arr, a, mid, fi);
                getadd( arr, a, la, mid);
                break;
            }

            else if(fi==mid||mid==la&&arr[mid]!=a){
                break;
            }
    }
        System.out.println("ww="+ww.getAndIncrement());
    }
    public void getdec(int[] arr,int a,int la,int fi){
        if(la==0&&fi==0){
         la=arr.length-1;
         fi=0;}
        while (fi<la){
            int mid =fi+(la-fi)/2;
            if(arr[mid]<a&&arr[mid]<=arr[fi]&&arr[mid]>=arr[la]){
                la=mid-1;
                continue;
            }else if(arr[mid]>=a&&arr[mid]<=arr[fi]&&arr[mid]>=arr[la]){
                fi=mid+1;
                continue;
            }else if(arr[mid]==a){
                System.out.println(mid);
                break;
            }
            else if(arr[mid]>=arr[fi]&&arr[mid]<=arr[la]){
                getadd( arr, a, la, fi);
                 break;
            }else if(arr[mid]>=arr[fi]&&arr[mid]>=arr[la]){
                getdec( arr, a, la, mid);
                getadd( arr, a, mid, fi);
                break;
            }else if(arr[mid]<=arr[fi]&&arr[mid]<=arr[la]){
                getdec( arr, a, mid, fi);
                getadd( arr, a, la, mid);
                break;
            }
          else if(fi==mid||mid==la&&arr[mid]!=a){
                break;
            }
        }
        System.out.println("ww="+ww.getAndIncrement());
    }

}
class client1{
    public static void main(String[] args) {
    //    int[] arr={1,2,3,2,1,0,0};
        E1d4d20 a=new E1d4d20();
        int[] arr = {1,2,8,6,4,3,0};
        a.getadd(arr,8,0,0);
        }
}
